Showing posts with label Bash Programming. Show all posts
Showing posts with label Bash Programming. Show all posts

Saturday, 28 January 2017

How to Create Loop in Shell Scripting in ubuntu

Harry

Flow Control - Part 1

In this lesson, we will look at how to add intelligence to our scripts. So far, our script has only consisted of a sequence of commands that starts at the first line and continues line by line until it reaches the end. Most programs do more than this. They make decisions and perform different actions depending on conditions.
The shell provides several commands that we can use to control the flow of execution in our program. These include:
  • if
  • exit
  • for
  • while
  • until
  • case
  • break
  • continue

if

The first command we will look at is if. The if command is fairly simple on the surface; it makes a decision based on a condition. The if command has three forms:
# First form

if condition ; then
    commands
fi

# Second form

if condition ; then
    commands
else
    commands
fi

# Third form

if condition ; then
    commands
elif condition ; then
    commands
fi
       
In the first form, if the condition is true, then commands are performed. If the condition is false, nothing is done.
In the second form, if the condition is true, then the first set of commands is performed. If the condition is false, the second set of commands is performed.
In the third form, if the condition is true, then the first set of commands is performed. If the condition is false, and if the second condition is true, then the second set of commands is performed.

What is a "condition"?

To be honest, it took me a long time to really understand how this worked. To help answer this, there is yet another basic behavior of commands we must discuss.

Exit status

A properly written Unix application will tell the operating system if it was successful or not. It does this by means of an exit status. The exit status is a numeric value in the range of 0 to 255. A "0" indicates success; any other value indicates failure. Exit status provides two important features. First, it can be used to detect and handle errors and second, it can be used to perform true/false tests.
It is easy to see that handling errors would be valuable. For example, in our script we will want to look at what kind of hardware is installed so we can include it in our report. Typically, we will try to query the hardware, and if an error is reported by whatever tool we use to do the query, our script will be able to skip the portion of the script which deals with the missing hardware.
We can also use the exit status to perform simple true/false decisions. We will cover this next.

test

The test command is used most often with the if command to perform true/false decisions. The command is unusual in that it has two different syntactic forms:
# First form

test expression

# Second form

[ expression ]
       
The test command works simply. If the given expression is true, test exits with a status of zero; otherwise it exits with a status of 1.
The neat feature of test is the variety of expressions you can create. Here is an example:
if [ -f .bash_profile ]; then
    echo "You have a .bash_profile. Things are fine."
else
    echo "Yikes! You have no .bash_profile!"
fi
       
In this example, we use the expression " -f .bash_profile ". This expression asks, "Is .bash_profile a file?" If the expression is true, then test exits with a zero (indicating true) and the if command executes the command(s) following the word then. If the expression is false, then test exits with a status of one and the if command executes the command(s) following the word else.
Here is a partial list of the conditions that test can evaluate. Since test is a shell builtin, use "help test" to see a complete list.


Expression
Description
-d file
True if file is a directory.
-e file
True if file exists.
-f file
True if file exists and is a regular file.
-L file
True if file is a symbolic link.
-r file
True if file is a file readable by you.
-w file
True if file is a file writable by you.
-x file
True if file is a file executable by you.
file1 -nt file2
True if file1 is newer than (according to modification time) file2
file1 -ot file2
True if file1 is older than file2
-z string
True if string is empty.
-n string
True if string is not empty.
string1 = string2
True if string1 equals string2.
string1 != string2
True if string1 does not equal string2.
Before we go on, I want to explain the rest of the example above, since it also reveals more important ideas.
In the first line of the script, we see the if command followed by the test command, followed by a semicolon, and finally the word then. I chose to use the [ expression ] form of the test command since most people think it's easier to read. Notice that the spaces between the "[" and the beginning of the expression are required. Likewise, the space between the end of the expression and the trailing "]".
The semicolon is a command separator. Using it allows you to put more than one command on a line. For example:
[me@linuxbox me]$ clear; ls
will clear the screen and execute the ls command.
I use the semicolon as I did to allow me to put the word then on the same line as the if command, because I think it is easier to read that way.
On the second line, there is our old friend echo. The only thing of note on this line is the indentation. Again for the benefit of readability, it is traditional to indent all blocks of conditional code; that is, any code that will only be executed if certain conditions are met. The shell does not require this; it is done to make the code easier to read.
In other words, we could write the following and get the same results:
# Alternate form

if [ -f .bash_profile ]
then
    echo "You have a .bash_profile. Things are fine."
else
    echo "Yikes! You have no .bash_profile!"
fi

# Another alternate form

if [ -f .bash_profile ]
then echo "You have a .bash_profile. Things are fine."
else echo "Yikes! You have no .bash_profile!"
fi
       

exit

In order to be good script writers, we must set the exit status when our scripts finish. To do this, use the exit command. The exit command causes the script to terminate immediately and set the exit status to whatever value is given as an argument. For example:
exit 0
       
exits your script and sets the exit status to 0 (success), whereas
exit 1
       
exits your script and sets the exit status to 1 (failure).

Testing for root

When we last left our script, we required that it be run with superuser privileges. This is because the home_space function needs to examine the size of each user's home directory, and only the superuser is allowed to do that.
But what happens if a regular user runs our script? It produces a lot of ugly error messages. What if we could put something in the script to stop it if a regular user attempts to run it?
The id command can tell us who the current user is. When executed with the "-u" option, it prints the numeric user id of the current user.
[me@linuxbox me]$ id -u
501
[me@linuxbox me]$ su
Password:
[root@linuxbox me]# id -u
0
If the superuser executes id -u, the command will output "0." This fact can be the basis of our test:
if [ $(id -u) = "0" ]; then
    echo "superuser"
fi
       
In this example, if the output of the command id -u is equal to the string "0", then print the string "superuser."
While this code will detect if the user is the superuser, it does not really solve the problem yet. We want to stop the script if the user is not the superuser, so we will code it like so:
if [ $(id -u) != "0" ]; then
    echo "You must be the superuser to run this script" >&2
    exit 1
fi
       
With this code, if the output of the id -u command is not equal to "0", then the script prints a descriptive error message, exits, and sets the exit status to 1, indicating to the operating system that the script executed unsuccessfully.
Notice the ">&2" at the end of the echo command. This is another form of I/O direction. You will often notice this in routines that display error messages. If this redirection were not done, the error message would go to standard output. With this redirection, the message is sent to standard error. Since we are executing our script and redirecting its standard output to a file, we want the error messages separated from the normal output.
We could put this routine near the beginning of our script so it has a chance to detect a possible error before things get under way, but in order to run this script as an ordinary user, we will use the same idea and modify the home_space function to test for proper privileges instead, like so:
function home_space
{
    # Only the superuser can get this information

    if [ "$(id -u)" = "0" ]; then
        echo "<h2>Home directory space by user</h2>"
        echo "<pre>"
        echo "Bytes Directory"
            du -s /home/* | sort -nr
        echo "</pre>"
    fi

}   # end of home_space
       
This way, if an ordinary user runs the script, the troublesome code will be passed over, rather than executed and the problem will be solved.

Thursday, 26 January 2017

Introduction to PERL Programming

Anonymous

Introduction to PERL Programming

What is Perl?

Perl is a programming language especially  designed for text editing. It is now widely used for a variety of purposes including Linux system administration, network programming, web development etc.
Perl is of great importance in a Linux operating system where it can be used to create programs, handle Databases and e-mails, GUI (Graphical User Interface) development, Networking and System Administration.
 
Please be patient. The Video will load in some time. If you still face issue viewing video click here

PERL V/s Shell Scripting

Introduction to PERL Programming
Even though, shell scripting is available to programmers, they prefer Perl because:
  • Programming on Perl does not cause portability issues, which is common when using different shells in shell scripting.
  • Error handling is very easy on Perl
  • You can write long and complex programs on Perl easily due to its vastness. This is in contrast with Shell that does not support namespaces , modules , object , inheritance etc.
  • Shell has fewer reusable libraries available . Nothing compared to Perl's CPAN
  • Shell is less secure. Its calls external functions(commands like mv , cp etc depend on the shell being used) . On the contrary PERL does useful work while using internal functions.

Perl Basics

Always start your script with  
#!/usr/bin/perl
It directs the execution to Perl interpreter on your system.
Introduction to PERL Programming
The path is usually the same on most of the Linux distributions.
 

Storing Variables, Input and Output

Action Description Syntax Example
Defining a Variable value Storing values to a Variable in form of string and number $variable = "value"; $name = "Ronald";
Output in Perl If you want a string or a value to display on the screen then you can use the print command print ("value to be printed") ;   Print("thanks");
Input in Perl If you want a user input to be assigned to a variable use $variable = ; $username = ;
Important points
  • With this, if you want the Perl interpreter to ignore a statement, prefix it with a # symbol.
  • Remember that every statement in Perl ends with a semi-colon.
  • Perl is case-sensitive . Make sure you use the right case.
  • You can use any text editor to write your PERL scripts.
  • You should then save the script file in .pl extension which will make it recognizable.
  • Make sure you do not use spaces when you are naming the Perl script file.
 
Introduction to PERL Programming

Creating a PERL Script

Let us understand the steps in creating a PERL Script
 
  1. Create a file using a vi editor(or any other editor).  Name  script file with extension .pl
  2. Start the script with #! /bin/perl
  3. Write some code.
  4. Save the script file  as filename.pl
  5. For executing the script type perl filename.pl
 
Let's write a PERL script which will take input from the user and display it back through the script.
#!/usr/bin/perl
print("May I take your name please?") ;
$name = ;
print("Thank you $name");
 
Let's see the steps to create this script -
Introduction to PERL Programming
Summary:
  • Perl is a general-purpose programming language originally developed for text manipulation
  • Now used for a wide range of tasks including system administration, web development, network programming, GUI development, and more.
  • Perl files have .pl extension
  • There are three types of variables in Perl, Scalar, Lists and Hashes.

Introduction to Shell Scripting

Anonymous

What is a Shell?

An Operating is made of many components but its  two prime components are -
  • Kernel
  • Shell
Introduction to Shell Scripting
Please be patient. The Video will load in some time. If you still face issue viewing video click here
Kernel is at the nucleus of a computer. It makes the communication between the hardware and software possible. While the Kernel is the innermost part of an operating system, a shell is the outermost one.
A shell in a Linux operating system takes input from you in the form of commands, processes it, and then gives an output. It is the interface through which a user works on the programs, commands and scripts. A shell is accessed by a terminal which runs it.
When you run the terminal, the Shell issues a command prompt (usually $), where you can type your input, which is then executed when you hit the Enter key. The output or the result is thereafter displayed on the terminal.
The Shell wraps around the delicate interior of an Operating system protecting it from accidental damage. Hence the name Shell.

Types of Shell

There are two main shells in Linux:
1. The Bourne Shell: The prompt for this shell is $ and its derivatives are listed below:
  • POSIX shell  also known as sh
  • Korn Shell also known as sh
  • Bourne Again SHell also known as bash (most popular)
2. The C shell: The prompt for this shell is % and its subcategories are:
  • C shell also known as csh
  • Tops C shell also known as tcsh
We will discuss bash shell based shell scripting in this tutorial.

What is Shell scripting and why do I need it?

Writing a series of command for the shell to execute is called shell scripting.It can combine lengthy and repetitive sequences of commands into a single and simple script, which can be stored and executed anytime. This reduces the effort required by the end user..
Let us understand the steps in creating a Shell Script
  1. Create a file using a vi editor(or any other editor).  Name  script file with extension .sh
  2. Start the script with #! /bin/sh
  3. Write some code.
  4. Save the script file as filename.sh
  5. For executing the script type bash filename.sh
"#!" is an operator called shebang which directs the script to the interpreter location. So, if we use"#! /bin/sh" the script gets directed to the bourne-shell.
Let  create a small script -
#!/bin/sh
ls
Let's see the steps to create it -
Introduction to Shell Scripting
 
Command 'ls' is executed when we execute the scripsample.sh file.

Adding shell comments

Commenting is important in any program. In Shell, the syntax to add a comment is
#comment
Let understand this with an example
Introduction to Shell Scripting

What are Shell Variables?

As discussed earlier, Variables store data in the form of characters and numbers. Similarly, Shell variables are used to store information and they can by the shell only.
For example, the following creates a shell variable and then prints it:
variable ="Hello"
echo $variable
Below is a  small script which will use a variable.
#!/bin/sh
echo "what is your name?"
read name
echo "How do you do, $name?"
read remark
echo "I am $remark too!"
Let's understand,  the steps to create and execute the script
Introduction to Shell Scripting
As you see, the program picked the value of the variable 'name' as Joy and 'remark' as excellent.
This is a simple script. You can develop advanced scripts which contain conditional statements, loops and functions.  Shell scripting will make your life easy and Linux administration a breeze. 
Introduction to Shell Scripting

Summary:

  • Kernel is the nucleus of the operating systems and it communicates between hardware and software
  • Shell is a program which interprets user commands through CLI like Terminal
  • The Bourne shell and the C shell are the most used shells in Linux
  • Shell scripting is writing a series of command for the shell to execute
  • Shell variables store the value of a string or a number for the shell to read
  • Shell scripting can help you create complex programs containing conditional statements, loops and functions